

package com.yungu.swift.system.sys.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.miemiedev.mybatis.paginator.domain.PageBounds;
import com.github.miemiedev.mybatis.paginator.domain.PageList;
import com.yungu.swift.base.ResponseData;
import com.yungu.swift.base.mapper.IMapper;
import com.yungu.swift.base.model.PageVo;
import com.yungu.swift.base.service.impl.BaseServiceImpl;
import com.yungu.swift.system.finance.model.enums.FinanceEnum;
import com.yungu.swift.system.finance.model.param.AdminAccountFlowPageParam;
import com.yungu.swift.system.finance.model.param.AdminAccountPageParam;
import com.yungu.swift.system.sys.dao.SysAccountCompanyMapper;
import com.yungu.swift.system.sys.model.dto.SysAccountCompanyDto;
import com.yungu.swift.system.sys.model.vo.SysCompanyAccountFlowVo;
import com.yungu.swift.system.sys.model.vo.SysCompanyAccountVo;
import com.yungu.swift.system.sys.service.SysAccountCompanyService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.Objects;

@Service
public class SysAccountCompanyServiceImpl extends BaseServiceImpl<SysAccountCompanyDto> implements SysAccountCompanyService {
    @Autowired
    private SysAccountCompanyMapper sysAccountCompanyMapper;

    @Override
    protected IMapper<SysAccountCompanyDto> getMapper() {
        return sysAccountCompanyMapper;
    }

    @Override
    public ResponseData<PageVo<SysCompanyAccountVo>> queryPageVo(AdminAccountPageParam pageParam) {
        PageBounds pageBounds = new PageBounds(pageParam.getPageNum(), pageParam.getPageSize());
        PageList<SysCompanyAccountVo> pageList = sysAccountCompanyMapper.queryPageVo(pageParam, pageBounds);
        PageVo<SysCompanyAccountVo> pageVo = new PageVo(pageParam.getPageNum(), pageParam.getPageSize(),
                pageList.getPaginator().getTotalCount());
        pageVo.setItems(pageList);
        return ResponseData.buildSuccessResponse(pageVo);
    }

    @Override
    public ResponseData<List<SysCompanyAccountVo>> export(AdminAccountPageParam pageParam) {
        return ResponseData.buildSuccessResponse(sysAccountCompanyMapper.queryPageVo(pageParam));
    }

    @Override
    public ResponseData<SysCompanyAccountVo> statistics(AdminAccountPageParam pageParam) {
        return ResponseData.buildSuccessResponse(sysAccountCompanyMapper.statistics(pageParam));
    }

    @Override
    public ResponseData<PageVo<SysCompanyAccountFlowVo>> flowQueryPage(AdminAccountFlowPageParam flowPageParam) {
        PageBounds pageBounds = new PageBounds(flowPageParam.getPageNum(), flowPageParam.getPageSize());
        PageList<SysCompanyAccountFlowVo> pageList = sysAccountCompanyMapper.flowQueryPage(flowPageParam, pageBounds);
        PageVo<SysCompanyAccountFlowVo> pageVo = new PageVo(flowPageParam.getPageNum(), flowPageParam.getPageSize(),
                pageList.getPaginator().getTotalCount());
        fillDate(pageList, flowPageParam.getUuid());
        pageVo.setItems(pageList);
        return ResponseData.buildSuccessResponse(pageVo);
    }

    @Override
    public ResponseData<List<SysCompanyAccountFlowVo>> flowExport(AdminAccountFlowPageParam flowPageParam) {
        List<SysCompanyAccountFlowVo> flowVoList = sysAccountCompanyMapper.flowQueryPage(flowPageParam);
        fillDate(flowVoList, flowPageParam.getUuid());
        return ResponseData.buildSuccessResponse(flowVoList);
    }

    /**
     * 补充数据含义
     *
     * @param list
     * @param uuid
     */
    private void fillDate(List<SysCompanyAccountFlowVo> list, String uuid) {
        list.stream().forEach(accountFlowVo -> {
            accountFlowVo.setCourseDescription(FinanceEnum.getMsg(accountFlowVo.getCourse()));
            accountFlowVo.setTransactionType(Objects.equals(accountFlowVo.getFinanceInflow(), uuid) ? "收入" : "支出");
        });
    }
}